package l1j.server.server.model.damage;

import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.gametime.L1GameTimeClock;
import l1j.server.server.model.poison.L1DamagePoison;
import l1j.server.server.model.poison.L1ParalysisPoison;
import l1j.server.server.model.poison.L1SilencePoison;
import l1j.server.server.model.skill.L1SkillId;
import l1j.server.server.utils.Random;

public class L1NpcDamage {
	private L1NpcInstance _attacker;
	
	private L1Character _target;
	
	private L1Dmg _dmg;
	
	public L1NpcDamage(L1NpcInstance attacker,L1Character target,L1Dmg dmg){
		_attacker = attacker;
		_target= target;
		_dmg = dmg;
	}
	
	public void calcNpcDmg(){
		int lvl = _attacker.getLevel();
		if (lvl < 10) {
			_dmg.add( Random.nextInt(lvl) + 10D + _attacker.getStr() / 2 + 1);
		} else {
			_dmg.add(Random.nextInt(lvl) + _attacker.getStr() / 2 + 1);
		}

		if (_attacker instanceof L1PetInstance) {
			_dmg.add((lvl / 16))
				.add(((L1PetInstance) _attacker).getDamageByWeapon());
		}

		_dmg.add(_attacker.getDmgup());

		int undead = _attacker.getNpcTemplate().get_undead();
		if(L1GameTimeClock.getInstance().currentTime().isNight() &&
				((undead == 1) || (undead == 3) || (undead == 4))
				){
			_dmg.mul(1.1);
		}
		
		_dmg.mul(_attacker.getLeverage() / 10 );
		
		// 武器屬性階位傷害
		L1Dmg attrEnchantDmg = L1Dmg.get(true, 0, _attacker, _target);
		attrEnchantDmg.commitAttrByWp(_dmg.getValue());
		_dmg.sub(attrEnchantDmg.getValue());
		
		// 烙印裝備符文系統 - 【虛】
		if (_attacker.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_5)) {
			double subDmg = _attacker.get_SubRuneSetEffect();
			if (_dmg.getValue() <= subDmg) {
				subDmg = _dmg.getValue() - 1;
			}
			_dmg.sub(subDmg);
		}
		if (_target.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_8)
				&& Random.chance100(33)) {
			_dmg.mul(1.5);
		}
	}
	
	public void isWeaponBreaked(){
		if(_attacker.isWeaponBreaked())
			_dmg.div(2);
	}
	
	// ●●●● ＮＰＣの毒攻撃を付加 ●●●●
	public void addNpcPoisonAttack() {
		if (_attacker.getNpcTemplate().get_poisonatk() != 0) { // 毒攻撃あり
			if (15 >= Random.nextInt(100) + 1) { // 15%の確率で毒攻撃
				if (_attacker.getNpcTemplate().get_poisonatk() == 1) { // 通常毒
					// 3秒周期でダメージ5
					L1DamagePoison.doInfection(_attacker, _target, 3000, 5);
				} else if (_attacker.getNpcTemplate().get_poisonatk() == 2) { // 沈黙毒
					L1SilencePoison.doInfection(_target);
				} else if (_attacker.getNpcTemplate().get_poisonatk() == 4) { // 麻痺毒
					// 20秒後に45秒間麻痺
					L1ParalysisPoison.doInfection(_target, 20000, 45000);
				}
			}
		} else if (_attacker.getNpcTemplate().get_paralysisatk() != 0) { // 麻痺攻撃あり
		}
	}
}
